package com.forezp.thinking.chapter9;

import javax.jws.Oneway;

/**
 * Created by forezp on 2017/12/1.
 */
public class Seauence {

    private Object[] items;
    private int next = 0;

    public Seauence(int size) {
        items = new Object[size];
    }

    public void add(Object o) {
        if (next < items.length) {
            items[next++] = o;
        }
    }

    public SequenceSelctor selector() {
        return new SequenceSelctor();
    }

    class SequenceSelctor implements Selector {

        int i = 0;

        @Override
        public boolean end() {
            return i == items.length;

        }

        @Override
        public void next() {
            if (i < items.length) {
                i++;
            }
        }

        @Override
        public Object current() {
            return items[i];
        }
    }

    public static void main(String[] args) {
        Seauence seauence = new Seauence(10);
        for (int i = 0; i < 10; i++) {
            seauence.add(i);
        }
        Selector selector = seauence.selector();
        while (!selector.end()) {
            System.out.println(selector.current());
            selector.next();
        }
    }
}
